--
-- Author: yawei.zhu
-- Date: 2018-06-23 17:46:40
--
local PvpChampionSearch = class("PvpChampionSearch")

function PvpChampionSearch:ctor(node)
	self._searchNode = node

	self:initNode()
end

function PvpChampionSearch:initNode()
	local searchPanel = self._searchNode:getChildByName("Panel")

	local tlt = searchPanel:findChild("Panel_tlt/tlt")
	tlt:setString( L("champion_team1") )

	local tltDesc = searchPanel:findChild("Panel_ani/tlt")
	tltDesc:setString( L("champion_team2") )

	-- 裁切搜索动画
	local aniPanel = searchPanel:getChildByName("Panel_ani")
	local di = aniPanel:getChildByName("di")
	di:retain()
	di:removeFromParent()

	local mask = aniPanel:getChildByName("mask")
	mask:retain()
	mask:removeFromParent()

	local cilpNode = cc.ClippingNode:create()
	cilpNode:setAlphaThreshold(0.5)
	cilpNode:setPosition(0, 0)
	cilpNode:addChild(di)
	cilpNode:setStencil(mask)
	cilpNode:addTo(aniPanel, -1)

	di:release()
	mask:release()

	local bomPanel = searchPanel:getChildByName("Panel_bom")
	local btnFound = bomPanel:getChildByName("Button_found")
	btnFound:setVisible(false)
	local buttonNode = bomPanel:getChildByName("Button_tips")
	buttonNode:getChildByName("Panel_1"):addClickEventListener(function()
		executeFunc(self._onOperate, "PvpChampion_search_2")
	end)

	local btnCancel = bomPanel:getChildByName("Button_enter")
	btnCancel:addClickEventListener(function()
		local confirmDialog = display.newConfirmDialog( L("champion_register4"), 
			function()
				executeFunc(self._onOperate, "PvpChampion_CancelMatch")
			end)
			UIHandler:addPopDialog(confirmDialog)
	end)

	self._bomText = bomPanel:getChildByName("Text")
	self._bomText:setString("")

	self._timeline = self._searchNode:getTimeline()
	self._timeline:gotoFrameAndPause(0)
end

function PvpChampionSearch:tick(endTime)
	local remainedTime = endTime - app.session:getServerTime()
	if remainedTime < 0 then remainedTime = 0 end
	local hour = math.floor(remainedTime / 3600)
	local min = math.floor((remainedTime - hour * 3600) / 60 )
	local sec = math.floor(remainedTime % 60)
	self._bomText:setString( string.format( L("champion_register5"), hour, min, sec ) )
end


function PvpChampionSearch:setShow(visible)
	self._searchNode:setVisible(visible)

	if visible then
		self._timeline:gotoFrameAndPlay(0, true)
	else
		self._timeline:gotoFrameAndPause(0)
	end
end

function PvpChampionSearch:isVisible()
	return self._searchNode:isVisible()
end


function PvpChampionSearch:onOperate(cb)
	self._onOperate = cb
end

return PvpChampionSearch

